package com.cishu.orancle.utils;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;

/**
 * 自定义Header
 *  Map<String, String> additionalHeaders = new HashMap<>();
 *  additionalHeaders.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
* */

public class HttpClientUtil {
    private static final HttpClient client = HttpClient.newHttpClient();
    private static final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36";
    private static final String ACCEPT = "application/json, text/plain, */*";
    private static final String ACCEPT_LANGUAGE = "zh-CN,zh;q=0.9";
    private static final String CONTENT_LANGUAGE = "zh_CN";
    private static final String PROXY_CONNECTION = "keep-alive";
    private static HttpRequest.Builder createRequestBuilder(String url, String method, String body,
                                                            String authorization, String cookie, Map<String, String> additionalHeaders) {
        HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .method(method, body != null ? HttpRequest.BodyPublishers.ofString(body) : HttpRequest.BodyPublishers.noBody())
                .setHeader("Accept", ACCEPT)
                .setHeader("Accept-Language", ACCEPT_LANGUAGE)
                .setHeader("Content-Language", CONTENT_LANGUAGE)
                .setHeader("Proxy-Connection", PROXY_CONNECTION)
                .setHeader("User-Agent", USER_AGENT);

        // 设置可选头
        if (authorization != null && !authorization.isEmpty()) {
            requestBuilder.setHeader("Authorization", authorization);
        }
        if (cookie != null && !cookie.isEmpty()) {
            requestBuilder.setHeader("Cookie", cookie);
        }

        // 添加额外的请求头
        if (additionalHeaders != null) {
            additionalHeaders.forEach(requestBuilder::setHeader);
        }

        return requestBuilder;
    }

    public static HttpResponse<String> POST(String url, String body, String authorization, String cookie, Map<String, String> additionalHeaders) throws Exception {
        HttpRequest request = createRequestBuilder(url, "POST", body, authorization, cookie, additionalHeaders).build();
        return client.send(request, HttpResponse.BodyHandlers.ofString());
    }
    public static HttpResponse<String> POST(String url) throws Exception {
        return POST(url, "", "", "", null);
    }

    public static HttpResponse<String> POST(String url, String body) throws Exception {
        return POST(url, body, "", "", null);
    }

    public static HttpResponse<String> POST(String url, String body, String authorization) throws Exception {
        return POST(url, body, authorization, "", null);
    }

    public static HttpResponse<String> POST(String url, String body, String authorization, String cookie) throws Exception {
        return POST(url, body, authorization, cookie, null);
    }


    public static HttpResponse<String> GET(String url, String authorization, String cookie, Map<String, String> additionalHeaders) throws Exception {
        HttpRequest request = createRequestBuilder(url, "GET", null, authorization, cookie, additionalHeaders).build();
        return client.send(request, HttpResponse.BodyHandlers.ofString());
    }

    public static HttpResponse<String> GET(String url) throws Exception {
        return GET(url, "", "", null);
    }

    public static HttpResponse<String> GET(String url, String authorization) throws Exception {
        return GET(url, authorization, "", null);
    }

    public static HttpResponse<String> GET(String url, String authorization, String cookie) throws Exception {
        return GET(url, authorization, cookie, null);
    }


}
